﻿using Microsoft.VisualBasic.Devices;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoClick.v1
{
    public partial class FmMain : Form
    {

        private bool isRunning = false;
        private const int HotKeyId = 5201;

        public FmMain()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 检查文本框输入内容是否为数字
        /// </summary>
        /// <param name="e"></param>
        private void CheckTextBoxInput(KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)) { e.Handled = true; }
        }

        private void SaveConfig()
        {
            SaveTbxDuration();
            SaveTbxInterval();
            Config.SaveConfig();
        }

        private void LoadConfig()
        {
            switch (Config.ConfigModel.MouseKey)
            {
                case MouseKey.Left:
                    rbtn_left.Checked = true;
                    break;
                case MouseKey.Middle:
                    rbtn_middle.Checked = true;
                    break;
                case MouseKey.Right:
                    rbtn_right.Checked = true;
                    break;
                default:
                    rbtn_left.Checked = true;
                    break;
            }
            if (Config.ConfigModel.ClickMode == ClickMode.Single)
            {
                rbtn_single.Checked = true;
            }
            else
            {
                rbtn_double.Checked = true;
            }
            tbx_duration.Text = Config.ConfigModel.Duration.ToString();
            tbx_interval.Text = Config.ConfigModel.Interval.ToString();
            SetStatus();
        }


        private void SaveTbxDuration()
        {
            var value = int.Parse(tbx_duration.Text.Trim());
            if (value < Config.ConfigModel.DurationMin)
            {
                Config.ConfigModel.Duration = Config.ConfigModel.DurationMin;
            }
            else if (value > Config.ConfigModel.DurationMax)
            {
                Config.ConfigModel.Duration = Config.ConfigModel.DurationMax;
            }
            else
            {
                Config.ConfigModel.Duration = value;
            }
            tbx_duration.Text = Config.ConfigModel.Duration.ToString();
        }

        private void SaveTbxInterval()
        {
            var value = int.Parse(tbx_interval.Text.Trim());
            if (value < Config.ConfigModel.IntervalMin)
            {
                Config.ConfigModel.Interval = Config.ConfigModel.IntervalMin;
            }
            else if (value > Config.ConfigModel.IntervalMax)
            {
                Config.ConfigModel.Interval = Config.ConfigModel.IntervalMax;
            }
            else
            {
                Config.ConfigModel.Interval = value;
            }
            tbx_interval.Text = Config.ConfigModel.Interval.ToString();
        }

        private void SetStatus()
        {
            lbl_status.ForeColor = Color.White;
            if (isRunning)
            {
                lbl_status.BackColor = Color.Green;
                lbl_status.Text = "运行中";
            }
            else
            {
                lbl_status.BackColor = Color.IndianRed;
                lbl_status.Text = "已停止";
            }
        }

        private void StartRun()
        {
            isRunning = true;
            SetStatus();
            lbl_msg.Text = "启动连点";
            panelMain.Enabled = false;
            timerAutoSave.Stop();
            timerMouseClick.Interval = Config.ConfigModel.Interval;
            timerMouseClick.Start();
        }

        private void StopRun()
        {
            isRunning = false;
            SetStatus();
            panelMain.Enabled = true;
            lbl_msg.Text = "停止连点";
            timerAutoSave.Start();
            timerMouseClick.Stop();
        }

        private void SetHotKey()
        {
            Win32API.RegisterHotKey(this.Handle, HotKeyId, Win32API.MOD_CONTROL | Win32API.MOD_ALT, Keys.G.GetHashCode());
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == Win32API.WM_HOTKEY)
            {
                switch (m.WParam.ToInt32())
                {
                    case HotKeyId:
                        if (isRunning)
                        {
                            StopRun();
                        }
                        else
                        {
                            StartRun();
                        }
                        break;
                    default:
                        break;
                }
            }

            base.WndProc(ref m);
        }


        private void rbtn_left_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtn_left.Checked)
            {
                Config.ConfigModel.MouseKey = MouseKey.Left;
            }
        }

        private void rbtn_middle_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtn_middle.Checked)
            {
                Config.ConfigModel.MouseKey = MouseKey.Middle;
            }
        }

        private void rbtn_right_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtn_right.Checked)
            {
                Config.ConfigModel.MouseKey = MouseKey.Right;
            }
        }

        private void rbtn_single_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtn_single.Checked)
            {
                Config.ConfigModel.ClickMode = ClickMode.Single;
            }
        }
        private void rbtn_double_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtn_double.Checked)
            {
                Config.ConfigModel.ClickMode = ClickMode.Double;
            }
        }

        private void tbx_duration_KeyPress(object sender, KeyPressEventArgs e)
        {
            CheckTextBoxInput(e);
        }

        private void tbx_interval_KeyPress(object sender, KeyPressEventArgs e)
        {
            CheckTextBoxInput(e);
        }

        private void timerAutoSave_Tick(object sender, EventArgs e)
        {
            SaveConfig();
            lbl_msg.Text = string.Empty;
        }

        private void timerMouseClick_Tick(object sender, EventArgs e)
        {
            if (Config.ConfigModel.ClickMode == ClickMode.Single)
            {
                Win32API.MouseClick(Config.ConfigModel.MouseKey);
            }
            else
            {
                Win32API.MouseDoubleClick(Config.ConfigModel.MouseKey);
            }
        }

        private void FmMain_Load(object sender, EventArgs e)
        {
            Config.LoadConfig();
            LoadConfig();
            timerAutoSave.Start();
            SetHotKey();
        }

        private void FmMain_KeyDown(object sender, KeyEventArgs e)
        {
            // 组合键 Ctrl+S
            if (e.KeyCode == Keys.S && e.Modifiers == Keys.Control)
            {
                SaveConfig();
                lbl_msg.Text = "保存成功";
            }
        }

        private void FmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Win32API.UnregisterHotKey(this.Handle, HotKeyId);
        }
    }
}
